import sys
import os

commons = ["Tileset.cs",
           "MapDisplayer.cs",
           "MouseFollower.cs",
           "lzw.cs",]

editorSpec = ["editor.cs",
              "TilePalette.cs"]

editor = commons + editorSpec

clientSpec = ["client.cs",
              "ClientConnection.cs"]

client = commons + clientSpec

all_code = commons + editorSpec + clientSpec

def check_more_recent(path,dependencies):
    mtime = os.stat(path).st_mtime
    for i in dependencies:
        if os.stat(path).st_mtime>mtime:
            return True
    return False

if __name__=="__main__":

    ### Editor.js
    print 'Making "editor.js" ...'
    code = "\n".join([file("src/"+i).read() for i in editor])
    file("../.tmpbuild/editor.cs",'w').write(code)
    s = "coffee -o ../dist/data/commons -c ../.tmpbuild/editor.cs"
    os.system(s)
    print 'Pushing editor files...'
    os.system("cp editor/* ../dist/editor/")

    print 'Making "client.js" ...'
    code = "\n".join([file("src/"+i).read() for i in client])
    file("../.tmpbuild/client.cs",'w').write(code)
    s = "coffee -o ../dist/data/commons -c ../.tmpbuild/client.cs"
    os.system(s)
    print 'Pushing client files...'
    os.system("cp client/* ../dist/client/")
    print 'Pushing common php...'
    os.system("cp src/*.php ../dist/data/")
    print 'Pushing protocol...'
    #os.system("cp ../Protocol/bit_protocol.py ../dist/data/bit_protocol")
    d = file("../Protocol/bit_protocol.py",'r').read().replace("\n","").replace(" ","").replace(",]","]")
    file("../dist/data/bit_protocol",'w').write(d)

    if 0:
        os.system("java -jar tmpbuild/google-closure/compiler.jar --js=data/commons/editor.js --js_output_file=data/commons/editor.min.js")

    ### Tiles.js
    print 'Making "tiles.js" ...'
    tiles = [os.path.join("data/tiles/",i) for i in os.listdir("../dist/data/tiles")if i.endswith(".png")]
    if check_more_recent("../dist/data/commons/tiles.js",tiles) or 1:
        json_str = "TilePaths = {"
        for t in tiles:
            json_str+='"'+os.path.split(t)[1].split("@")[0]+'":'+\
                '"../'+t+'",'
        json_str+="};\n"
        file("../dist/data/commons/tiles.js",'w').write(json_str)

    ### DOC
    print "Making documentation..."
    code = " "+"\n".join([file("src/"+i).read() for i in all_code])
    doc = """<html>
<head>
<title>Mauve Sea</title>
<style>
.note{
   font-size: 12px;
   font-family: "Courier New";
   margin: 5px;
}
.usage{
   background-color: #eeddee;
   border-radius: 10px;
   margin: 3px;
   padding: 3px;
}
.htmldiv{
   background-color: #ddeeee;
   border-radius: 10px;
   margin: 3px;
   padding: 3px;
}
</style>
</head><body>"""
    topics = code.split("###\n")[1::2]
    for topic in topics:
        for l in [i[1:].strip() for i in topic.splitlines()]:
            firstword = l[:l.index(" ")]
            if firstword=="-class":
                doc+="<h3>class "+l[len(firstword)+1:]+"</h3>"
            elif firstword=="-usage":
                doc+="<div class='note'><span class='usage'>Used in: "+l[len(firstword)+1:]+"</span></div>"
            elif firstword=="-htmldiv":
                doc+="<div class='note'><span class='htmldiv'>Makes use of element with id "+l[len(firstword)+1:]+"</span></div>"
            else:
                doc+=l+" "
        doc+="<br/>"
        doc+="<hr width='10%'/>"
    doc+="</body></html>"
    file("doc.html",'w').write(doc)
    print "Done."
